/**
 * Created by PengWei on 2018/7/26.
 */


namespace game {
    import BaseCommand = mengdao.BaseCommand;
    import TypeIdentifier = mengdao.TypeIdentifier;
    import BaseView = mengdao.BaseView;
    import BaseMediator = mengdao.BaseMediator;

    export class OpenUICommand extends BaseCommand {
        private _layer: Layer;
        private _viewInstance: BaseView<any>;
        private _mediatorInstance: BaseMediator;
        private _popStack:BaseMediator[]=[]

        public execute(data: any) {
            this.get(WindowManager).openWindow(data)
            // this._layer = this._injector.getFromBind(Layer);
            // this.executeInjection(data);
            // this.executeOpen();
        }

        /**
         *  框架注入激活
         */
        private executeInjection(data): void {
            let viewClass: TypeIdentifier<BaseView<any>> = data;
            let viewInstance = this._injector.getFromBind(viewClass);
            let isPop = viewInstance instanceof BasePopView;

            let mediatorClass = this._injector.getFromMap(viewClass);
            let mediatorInstance:BaseMediator = this._injector.getFromBind(mediatorClass);

            mediatorInstance.__setViewReadyCall(()=>{
                let parent = isPop ? this._layer.popUIContainer : this._layer.uiContainer;
                viewInstance.addToParent(parent);
            }, this);
            mediatorInstance.active();

            if(isPop){
                if(this._popStack.indexOf(mediatorInstance)<0){
                    this._popStack.push(mediatorInstance);
                }
            }else{
                if(this._mediatorInstance){
                    this._mediatorInstance.close();
                }
                this._viewInstance = viewInstance;
                this._mediatorInstance = mediatorInstance;
            }
        }

        /**
         *  打开面板
         */
        private executeOpen(): void {
            let parent = this._layer.popUIContainer;
            this._viewInstance.addToParent(parent);

            //TODO 各种UI打开时的处理、兼容、互斥等处理
        }
    }
}